<?php

namespace Admin\Model;

use Think\Model;

class OrdersModel extends Model {

    protected $trueTableName = 'tb_orders';

    public function getFind($where) {
        $result = $this->where($where)->find();
        return $result;
    }

    public function getCount($where) {
        $result = $this->where($where)->count();
        return $result;
    }

    public function getList($where, $param) {
        $field = "a.id,orderId as order_no,b.name,b.avatar,price,realpayprice,a.createDT,a.updatedt as updateDT,pickuptime,paytype,gorderstatus,guserstatus,ordertype,s_mobile,wx_time_end";
        $count = $this->alias('a')
                ->join("left join `tb_users` as b on a.userid=b.id")
                ->field($field)
                ->where($where)
                ->count();
        $Page = new \Think\Page($count, 15, $param); // 实例化分页类 传入总记录数和每页显示的记录数
        $list = $this->alias('a')
                ->join("left join `tb_users` as b on a.userid=b.id")
                ->field($field)
                ->where($where)
                ->limit($Page->firstRow . ',' . $Page->listRows)
                ->order("createDT DESC")
                ->select();
        return array('list' => $list, 'page' => $Page->show(), 'count' => $count);
    }

    public function getListForExcel($where, $param) {
        $field = "a.id,orderId as order_no,b.name,b.avatar,price,realpayprice,a.createDT,a.updatedt as updateDT,pickuptime,paytype,gorderstatus,guserstatus,ordertype,s_mobile,wx_time_end";
        $Page = new \Think\Page($count, 10, $param); // 实例化分页类 传入总记录数和每页显示的记录数
        $list = $this->alias('a')
                ->join("left join `tb_users` as b on a.userid=b.id")
                ->field($field)
                ->where($where)
                ->order("createDT DESC")
                ->select();
        return array('list' => $list, 'count' => 1);
    }

    public function saveData($where, $data) {
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function addData($data) {
        $result = $this->add($data);
        return $result;
    }

    public function getProductList($where) {
        $list = $this->alias('a')
                ->join("left join `tb_orders_detail` as b on a.orderId=b.orderId")
                ->where($where)
                ->field("b.productId,b.skuid,SUM(b.count) as count,b.ptitle")
                ->group("b.productId")
                ->select();
        return array('list' => $list);
    }

}
